/**
 * 用户接口操作数据库
 */

const connection = require("../app/database");
class UserService {
  async create(user) {
    //解构出name\password
    const { name, password } = user;
    //用户注册SQL
    const statement = `INSERT INTO users (name,password) VALUES (?,?);`;
    // 执行sql语句并返回结果
    const result = await connection.execute(statement, [name, password]);
    return result[0];
  }

  async getUserByName(name) {
    const statement = `SELECT * FROM users WHERE name =?`;
    const result = await connection.execute(statement, [name]);
    return result[0];
  }
// 把头像添加到数据库中
  async updateAvatarUrlById(avatarUrl, userId) {
    const statement = `UPDATE users SET avatar_url = ? WHERE id = ?;`;
    const [result] = await connection.execute(statement, [avatarUrl, userId]);
    return result;
  }
}
// 导出类
module.exports = new UserService();
